package com.ycsx.core.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * @author liruofei
 * @version Aug 29, 2010
 */
public class OmitStringTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 406117061358974344L;

	@Override
	public int doStartTag() throws JspException {
		
		return SKIP_BODY;
	}
	
	@Override
	public int doEndTag() throws JspException {

		try {
			pageContext.getOut().write( getOmitString(getValue(), getSize()));
		} catch (IOException e) {
			throw new JspException(e);
		}
		return EVAL_PAGE;
	}
	
	private String getOmitString(String value,int size) {
		if(value != null) {
			if(size > 0 && value.length() > size) {
				return value.substring(0, size - 1) + " ...";
			} else {
				return value;
			}
		}
		return "";
	}

	private String value;
	
	private String suffix = " ...";
	
	private int size = 10;

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public String getSuffix() {
		return suffix;
	}

	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
	
}
